<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_etablissement=new etablissement();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["id"])){$_etablissement->set_id($_POST["id"]);}
		if(isset($_POST["iduniversite"])){$_etablissement->set_iduniversite($_POST["iduniversite"]);}
		if(isset($_POST["intitulefr"])){$_etablissement->set_intitulefr($_POST["intitulefr"]);}
		if(isset($_POST["abrev"])){$_etablissement->set_abrev($_POST["abrev"]);}
		if(isset($_POST["gid"])){$_etablissement->set_gid($_POST["gid"]);}
		$res=$_etablissement->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["id"])){
		$_etablissement->set_id($_POST["id"]);
		$res=$_etablissement->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["id"])){$_etablissement->set_id($_POST["id"]);}
		if(isset($_POST["iduniversite"])){$_etablissement->set_iduniversite($_POST["iduniversite"]);}
		if(isset($_POST["intitulefr"])){$_etablissement->set_intitulefr($_POST["intitulefr"]);}
		if(isset($_POST["abrev"])){$_etablissement->set_abrev($_POST["abrev"]);}
		if(isset($_POST["gid"])){$_etablissement->set_gid($_POST["gid"]);}
		$res=$_etablissement->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["id"]) && !empty($_POST["id"])){
		$constraint.=" AND id='".$_POST["id"]."'";
	}

	if(isset($_POST["iduniversite"]) && !empty($_POST["iduniversite"])){
		$constraint.=" AND iduniversite='".$_POST["iduniversite"]."'";
	}

	if(isset($_POST["intitulefr"]) && !empty($_POST["intitulefr"])){
		$constraint.=" AND intitulefr='".$_POST["intitulefr"]."'";
	}

	if(isset($_POST["abrev"]) && !empty($_POST["abrev"])){
		$constraint.=" AND abrev='".$_POST["abrev"]."'";
	}

	if(isset($_POST["gid"]) && !empty($_POST["gid"])){
		$constraint.=" AND gid='".$_POST["gid"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_etablissement->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_etablissement->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>